package com.duowei.kitchenshow.fragment.setting;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;

import com.duowei.kitchenshow.R;
import com.duowei.kitchenshow.app.MyApp;
import com.duowei.kitchenshow.bean.Kitchens;
import com.duowei.kitchenshow.utils.SpUtils;
import com.duowei.kitchenshow.utils.ToastUtil;

import org.litepal.crud.DataSupport;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class BaseSettingFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener,
        Preference.OnPreferenceClickListener, KitchenSelectFragment.KitchenSelect {

    private SharedPreferences mSpf;
    private EditTextPreference mEtIp;
    private Context mContext;
    private Preference mPKitchen;

    public BaseSettingFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = MyApp.getContext();
        addPreferencesFromResource(R.xml.preference_base);
        initPreference();
    }

    private void initPreference() {
        List<Kitchens> kitchensList = DataSupport.select("cbmc").where("select=?", "1").find(Kitchens.class);
        String kitchenName="";
        for(int i=0;i<kitchensList.size();i++){
            kitchenName+=kitchensList.get(i).getCbmc()+",";
        }

        mEtIp = (EditTextPreference) findPreference(getString(R.string.serverIp));
        mPKitchen = findPreference(getString(R.string.kitchen));
        mPKitchen.setSummary(kitchenName);
        mPKitchen.setOnPreferenceClickListener(this);

        mSpf = getPreferenceScreen().getSharedPreferences();
        mEtIp.setSummary(mSpf.getString(mContext.getString(R.string.serverIp),""));

        mSpf.registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if(key.equals(getString(R.string.serverIp))){
            String ip = sharedPreferences.getString(getString(R.string.serverIp), "");
            mEtIp.setSummary(ip);
            SpUtils.put(getString(R.string.serverIp),ip);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSpf.unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        String ip = (String) SpUtils.get(getString(R.string.serverIp), "");
        if(TextUtils.isEmpty(ip)){
            ToastUtil.showToast("请先设置服务器IP");
        }else{
            KitchenSelectFragment fragment = KitchenSelectFragment.newInstance(ip);
            fragment.show(getFragmentManager(),null);
            fragment.setOnKitchenSelectListener(this);
        }
        return false;
    }

    @Override
    public void onKitchenSelect(List<Kitchens> kitchensList) {
        String str="";
        for(int i=0;i<kitchensList.size();i++){
            Kitchens kitchens = kitchensList.get(i);
            if(kitchens.isSelect()){
                str+=kitchens.getCbmc()+",";
            }
        }
        mPKitchen.setSummary(str);
    }
}
